/*****
 * BEGIN LICENSE BLOCK ***** Version: MPL 1.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations under
 * the License.
 * 
 * The Original Code is BzStats.
 * 
 * The Initial Developer of the Original Code is Michel de Bree (youth). Portions created by the Initial
 * Developer are Copyright (C) 2005 the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s):
 * 
 ***** END LICENSE BLOCK
 *****/

package bzstats;

import java.util.HashMap;

/**
 * Represents statistics for one player over a period of time.
 * 
 * @author youth
 * 
 */
public class PlayerStats implements Cloneable {

	private final String name;

	private int kills;

	private int deaths;

	private int wins;

	private int suicides;
	
    private int games;	

	private HashMap killerMap;

	private HashMap flagStatsKilled;

	private HashMap flagStatsLost;

	/**
	 * Constructor.
	 * 
	 * @param name The player's name
	 */
	public PlayerStats(String name) {
		this.name = name;
		kills = 0;
		deaths = 0;
		suicides = 0;
		killerMap = new HashMap();
		flagStatsKilled = new HashMap();
		flagStatsLost = new HashMap();
	}

	public HashMap getKillerMap() {
		return killerMap;
	}

	public HashMap getFlagStatsKilled() {
		return flagStatsKilled;
	}

	public HashMap getFlagStatsLost() {
		return flagStatsLost;
	}

	public void addKiller(String name) {
		if (killerMap.containsKey(name)) {
			Integer i = (Integer) killerMap.get(name);
			killerMap.put(name, i + 1);
		} else {
			killerMap.put(name, 1);

		}
	}

	public void addFlagKilled(String name) {
		if (flagStatsKilled.containsKey(name)) {
			Integer i = (Integer) flagStatsKilled.get(name);
			flagStatsKilled.put(name, i + 1);
		} else {
			flagStatsKilled.put(name, 1);

		}
	}

	public void addFlagLost(String name) {
		if (flagStatsLost.containsKey(name)) {
			Integer i = (Integer) flagStatsLost.get(name);
			flagStatsLost.put(name, i + 1);
		} else {
			flagStatsLost.put(name, 1);

		}
	}

	public void setKillerMap(HashMap killerMap) {
		this.killerMap = killerMap;
	}

	public void setFlagStatsKilled(HashMap flagstats) {
		this.flagStatsKilled = flagstats;
	}

	public void setFlagStatsLost(HashMap flagstats) {
		this.flagStatsLost = flagstats;
	}

	/**
	 * The number of times the player got killed. (excluding suicides).
	 * 
	 * @return Returns the deaths.
	 */
	public int getDeaths() {
		return deaths;
	}

	/**
	 * The number of times the player killed someone.
	 * 
	 * @return Returns the kills.
	 */
	public int getKills() {
		return kills;
	}

	/**
	 * The number of times the player has won.
	 * 
	 * @return Returns the wins.
	 */
	public int getWins() {
		return wins;
	}
	
    /**
     * The number of times the player played.
     * 
     * @return Returns the games.
     */
    public int getGames() {
        return games;
    }	

	/**
	 * The number of times the player killed himself.
	 * 
	 * @return Returns the suicides.
	 */
	public int getSuicides() {
		return suicides;
	}

	/**
	 * The name of the player.
	 * 
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * The score as defined in the game (kills - deaths).
	 * 
	 * @return The score.
	 */
	public int getScore() {
		return getKills() - getDeaths() - getSuicides();
	}

	/**
	 * The ratio of kills and deaths (including suicides).
	 * 
	 * @return Number of kills divided by number of deaths.
	 */
	public float getKillRatio() {

		float result = 0;

		if (getDeaths() > 0) {
			result = (float) getKills() / (float) (getDeaths() + getSuicides());
		}

		return result;
	}

	/**
	 * Add a number of wins to the statistics.
	 * 
	 * @param wins
	 */
	public void addWins(final int nrwins) {
		wins += nrwins;
	}
	
    /**
     * Add a number of games to the statistics.
     * 
     * @param wins
     */
    public void addGames(final int nrgames) {
        games += nrgames;
    }
	

	/**
	 * Add a number of kills to the statistics.
	 * 
	 * @param nrkills
	 */
	public void addKills(final int nrkills) {
		kills += nrkills;
	}

	/**
	 * Add a number of deaths to the statistics.
	 * 
	 * @param nrdeaths
	 */
	public void addDeaths(final int nrdeaths) {
		deaths += nrdeaths;
	}

	/**
	 * Add a number of suicides to the statistics.
	 * 
	 * @param nrsuicides
	 */
	public void addSuicides(final int nrsuicides) {
		suicides += nrsuicides;
	}

	public String toString() {
		return name + ": " + wins + " wins, " + games + " games, " + kills + " kills, " + deaths + " deaths, " + suicides
				+ " suicides, killratio: " + getKillRatio();

	}

	// /////////////////////////////////////////////////////////////////////////
	// Cloneable
	// ///////////////////////////////////////////////////////////////////////

	/** {@inheritDoc} */
	@Override
	protected Object clone() throws CloneNotSupportedException {
		PlayerStats clone = new PlayerStats(name);
		clone.deaths = deaths;
		clone.flagStatsKilled = (HashMap) flagStatsKilled.clone();
		clone.flagStatsLost = (HashMap) flagStatsLost.clone();
		clone.killerMap = (HashMap) killerMap.clone();
		clone.kills = kills;
		clone.suicides = suicides;
		clone.wins = wins;
        clone.games = games;		
		return clone;
	}

}
